<?php
namespace app\web\controller;
use app\web\model\User as Muser;
use think\Db;

/*
首页
提供整体后台框架输出
*/
class Index extends \think\Controller
{
	public function index(){
		// 检测登录状态
		if(!session('user_auth')){
			$this->redirect('Index/login');
		}
        //var_dump(session('user_auth'));
		
		if(!session('sidebar')){
			$this->getSidebar();
		}

		$this->redirect('Warehouse/Query/index');//跳转到默认页面
	}
	public function home(){
        return $this->fetch('home');
	}
    //登录页面
    public function login()
    {
		if(request()->isPost() && input('post.')){
			$username = input('post.username');
			$password = input('post.password');
			$code = input('post.code');
			//if(!$code){
			//	return $this->error('请填写验证码');
			//}
			//$phrase = session('phrase');
			//session('phrase', null);
			//if($phrase != $code){
			//	return $this->error('验证码错误');
			//}
			if(!$username || !$password){
				return $this->error('请填写用户名或密码');
			}
			$user = new Muser;
			$uid = $user->login($username, $password);
            //$uid = -1;
			if($uid>0){
				/*记录session和cookie*/
				$group_id = \think\Db::table('auth_group_access')->field('group_id')->where('uid',$uid)->find();
				$auth = [
					'uid'=>$uid,
					'group_id'=>$group_id['group_id'],
					'username'=>$username,
					'last_login_time'=>time(),
				];
				session('user_auth',$auth);
				session('user_uid',$uid);
				session('user_username',$username);
				session('user_auth_sign', data_auth_sign($auth));
				return $this->success('登录成功','Index/index');
			}else{
				switch ($uid) {
					case '-1':
						$error = '用户不存在或被禁用';
						break;
					case '-2':
						$error = '密码错误';
						break;
					
					default:
						$error = '未知错误';
						break;
				}
				return $this->error($error);
			}
		}else{
            return $this->fetch('login'); 
		}
           
    }
    //登出页面
    public function logout()
    {
		session(null);
		return $this->success('退出成功','index/index');
    }
	/**
	 * 获取菜单
	 * @author ning
	 * @DateTime 2016-06-22T21:44:16+0800
	 * @return   [type]                   [description]
	 */
	protected function getSidebar(){
		$authGroupAccessData = \think\Db::name('auth_group_access')->field('group_id')->where('uid',session('user_auth')['uid'])->find();
		$authGroupData = \think\Db::name('auth_group')->field('rules')->where('id',$authGroupAccessData['group_id'])->find();
		$authRuleData = \think\Db::name('auth_rule')->field('id,name,title,icon,pid,sort,path')->where('id','in',$authGroupData['rules'])->where('type',1)->where('status',1)->where('is_show',1)->order('path,sort asc')->select();
		$sidebar = [];
		foreach ($authRuleData as $key => $value) {
			$path = explode('-', $value['path']);
			switch(count($path)){
				case 1:
					$sidebar[$value['id']] = $value;
					break;
				case 2:
					$sidebar[$path[1]]['child'][$value['id']] = $value;
					break;
				case 3:
					$sidebar[$path[1]]['child'][$path[2]]['child'][$value['id']] = $value;
					break;
			}
		}
		session('sidebar',$sidebar);
	}

}
